<template>
  <el-drawer direction="rtl" v-model="dialogVisible" :title="dialogTitle" size="70%">
    <!-- 搜索 -->
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="120px"
      >
        <el-form-item label="电价方案名称" prop="keyWord">
          <el-input
            v-model="queryParams.keyWord"
            placeholder="请输入电价方案名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table v-loading="loading" :data="list">
        <el-table-column label="主键" align="center" prop="id" width="210" />
        <el-table-column label="方案名称" align="left" prop="name" />
        <el-table-column label="启用状态" align="center" prop="enable" width="120" />
        <el-table-column label="计费类型" align="cener" prop="priceTypeSn" width="120"/>
        <el-table-column label="方案类型" align="center" prop="typeSn" width="120"/>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="120"
        >
          <template #default="scope">
            <el-button type="primary" link @click="confirmHxnyPlmChargeTpl(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </el-drawer>
</template>
<script lang="ts" setup>
import { getHxnyPlmChargeTplPage } from '@/api/zwsj/meter'

defineOptions({ name: 'HxnyPlmChargeTpl' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const showHistoryFlag = ref(false)

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  keyWord: null
})

const queryFormRef = ref() // 搜索的表单

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  dialogTitle.value = '宏新能源电价方案'
  showHistoryFlag.value = false
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
      const data = await getHxnyPlmChargeTplPage(queryParams)
      list.value = data.list
      total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 确认 */
const emits = defineEmits(['confirmHxnyPlmChargeTpl'])
const confirmHxnyPlmChargeTpl = async (row: any) => {
  emits('confirm-hxny-plm-charge-tpl', row)
  dialogVisible.value = false
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
</script>
